﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RenderHeads.Media.AVProVideo;
using System.IO;
using System.Linq;

public class CardMono : BundleInit
{

    public MediaPlayer player1, player2;
    Renderer rd;
    bool isready;
    public System.Action ready_callback;
    bool FlipUVY;

    List<string> medias;
    PackageData currentPackageData;
    int curindex = 0;

    public override void Init()
    {
        rd = GetComponentInChildren<MeshRenderer>();
        player1 = transform.Find("maskplayer").GetComponent<MediaPlayer>();
        player2 = transform.Find("mainplayer").GetComponent<MediaPlayer>();

        currentPackageData = AppDataManager.Instance.GetCurrentPackageInfo();
        
        InitMediaDirs();
        // PlayVideo("C:/Users/bosssu/Downloads/muglife");

    }

    //便利目录=》播放
    private void InitMediaDirs()
    {
        // string dir = Path.Combine("D:/Git/unity_projects/rightdown-popup/workshop/gaoji","assets");
        Debug.Log(currentPackageData.packageDir + "/assets");
        string[] dirs = Directory.GetDirectories(Path.Combine(currentPackageData.packageDir,"assets"));
        // string[] dirs = Directory.GetDirectories(dir);
        if(dirs.Length > 0)
        {
            medias = dirs.ToList();
            PlayVideo(medias[0]);
        }
    }

    //==================================================================//

    public bool HasValidAlphaTexture()
    {
        return (player1 != null && player1.TextureProducer != null && player1.TextureProducer.GetTexture() != null);
    }

    public bool HasValidColorTexture()
    {
        return (player2 != null && player2.TextureProducer != null && player2.TextureProducer.GetTexture() != null);
    }

    public Texture colorTex
    {
        get
        {
            Texture result = Texture2D.whiteTexture;
            if (HasValidColorTexture())
            {
                result = player2.TextureProducer.GetTexture();
            }
            else
            {
                result = null;
            }
            return result;
        }
    }

    public Texture alphaTex
    {
        get
        {
            Texture result = Texture2D.whiteTexture;
            if (HasValidAlphaTexture())
            {
                result = player1.TextureProducer.GetTexture();
            }
            else
            {
                result = null;
            }
            return result;
        }
    }

    void Update()
    {
        if (!isready && HasValidAlphaTexture() && HasValidColorTexture())
        {
            if (ready_callback != null) ready_callback();
            isready = true;
        }

        if(Input.GetKeyDown(KeyCode.N))
        {
            curindex++;
            if(curindex > medias.Count - 1) curindex = 0;
            PlayVideo(medias[curindex]);
        }

    }

    public void PlayVideo(string videopath, bool isDefault = true)
    {
        bool issucess = PlayVideo(RenderHeads.Media.AVProVideo.MediaPlayer.FileLocation.AbsolutePathOrURL, videopath, true);
        if (issucess)
        {
            ready_callback = () =>
            {
                FlipUVY = player2.TextureProducer.RequiresVerticalFlip();
                Texture alphatex = alphaTex;
                Texture colortex = colorTex;
                rd.material.mainTexture = colortex;
                rd.material.SetTexture("_Mask",alphaTex);
                rd.material.SetFloat("flipflag",FlipUVY?1f:-1f);
                FixSize(new Vector2(alphaTex.width, alphaTex.height));
                lastAlphaTex = alphaTex;
                lastColorTex = colorTex;
            };
        }
    }

    private bool PlayVideo(MediaPlayer.FileLocation filelocation, string basepath, bool is_auto_play = false)
    {
        isready = false;
        string maskPath = Path.Combine(basepath, "alpha.mp4");
        string colorPath = Path.Combine(basepath, "color.mp4");
        bool p1s = player1.OpenVideoFromFile(filelocation, maskPath, is_auto_play);
        bool p2s = player2.OpenVideoFromFile(filelocation, colorPath, is_auto_play);
        return p1s && p2s;
    }

    void OnDestroy()
    {
        
    }
}
